<?php
use Webman\Route;

// 安装路由 - 必须在所有通配符路由之前
Route::get('/install', [app\install\controller\Install::class, 'index']);
Route::any('/install/config', [app\install\controller\Install::class, 'config']);
Route::post('/install/test-connection', [app\install\controller\Install::class, 'testConnection']);
Route::post('/install/install', [app\install\controller\Install::class, 'install']);
Route::get('/install/complete', [app\install\controller\Install::class, 'complete']);

// 检查是否已安装，只有安装后才读取配置
$lockFile = base_path() . '/.lock';
if (file_exists($lockFile)) {
    // 微信支付（直连模式）回调
    Route::post('/wxpay/notify/{id}', [app\api\controller\Pay::class, 'notify']);
    // 支付页面跳转
    Route::get('/gopay', [app\api\controller\Pay::class, 'pay']);
    // response
    Route::options('[{path:.+}]', function () {
        return response('');
    });
    // 公共异常页面
    Route::get('/exception', [app\api\controller\Index::class, 'exception']);
    // 根据站点模式决定使用哪种路由
    $siteMode = conf('app_site_mode');

    // 如果是HTML模式，注册HTML模板路由
    if ($siteMode === 'html') {
        // 前台页面路由 (使用think-template渲染)
        // 首页
        Route::get('/', [app\web\controller\Index::class, 'index']);
        // 商品详情页
        Route::get('/detail/{id}.html', [app\web\controller\Goods::class, 'detail'])->name('goods.detail');
        // 搜索结果页（查询参数优先）
        Route::get('/search', [app\web\controller\Goods::class, 'search']);
        Route::get('/search-{keyword}', [app\web\controller\Goods::class, 'search']);
        // 商品标签页
        Route::get('/tag', [app\web\controller\Goods::class, 'tag'])->name('goods.tag');
        Route::get('/tag/{id}', [app\web\controller\Goods::class, 'tagDetail']);
        // 分类接口
        Route::get('/web/categories', [app\web\controller\Goods::class, 'categories']);
        // 支付预览页
        Route::get('/pay-{id}', [app\web\controller\Pay::class, 'preview'])->name('pay.preview');
        // 用户认证
        Route::get('/user/login', [app\web\controller\Auth::class, 'login'])->name('user.login');
        Route::get('/user/register', [app\web\controller\Auth::class, 'register'])->name('user.register');
        Route::post('/user/doLogin', [app\web\controller\Auth::class, 'doLogin']);
        Route::post('/user/doRegister', [app\web\controller\Auth::class, 'doRegister']);
        Route::get('/user/logout', [app\web\controller\Auth::class, 'logout']);
        Route::get('/user/getUserInfo', [app\web\controller\Auth::class, 'getUserInfo']);
        // 用户中心
        Route::get('/user', [app\web\controller\User::class, 'index'])->name('user.index');
        Route::get('/user/index', [app\web\controller\User::class, 'index']);
        Route::get('/user/order', [app\web\controller\User::class, 'order'])->name('user.order');
        Route::get('/user/favorite', [app\web\controller\User::class, 'favorite'])->name('user.favorite');
        Route::get('/user/level', [app\web\controller\User::class, 'level'])->name('user.level');
        Route::get('/user/vip', [app\web\controller\User::class, 'vip'])->name('user.vip');
        Route::get('/user/sign', [app\web\controller\User::class, 'sign'])->name('user.sign');
        Route::get('/user/forgot', [app\web\controller\Auth::class, 'forgot'])->name('user.forgot');
        Route::post('/auth/sendResetEmail', [app\web\controller\Auth::class, 'sendResetEmail']);
        Route::get('/auth/resetPassword', [app\web\controller\Auth::class, 'resetPassword']);
        Route::post('/auth/doResetPassword', [app\web\controller\Auth::class, 'doResetPassword']);
        // 章节内容页
        Route::get('/chapter/{id}', [app\web\controller\Goods::class, 'chapter'])->name('goods.chapter');
        // 协议页面
        Route::get('/agreement', [app\web\controller\Agreement::class, 'page']);
        Route::post('/api/agreement', [app\web\controller\Agreement::class, 'index']);
        // AJAX接口路由
        Route::post('/pay/createOrder', [app\web\controller\Pay::class, 'createOrder']);
        Route::post('/user/doSign', [app\web\controller\User::class, 'doSign']);
        Route::post('/user/cancelOrder', [app\web\controller\User::class, 'cancelOrder']);
        // 评论相关路由
        Route::get('/goods/comments', [app\web\controller\Comment::class, 'lists']);
        Route::post('/comment/lists', [app\web\controller\Comment::class, 'lists']);
        Route::post('/comment/submit', [app\web\controller\Comment::class, 'submit']);
        Route::post('/comment/like', [app\web\controller\Comment::class, 'like']);
        Route::post('/comment/delete', [app\web\controller\Comment::class, 'delete']);

        // 商品分类页
        Route::get('/{pinyin}', [app\web\controller\Goods::class, 'category'])->name('goods.category');
        // 兼容旧版
        Route::get('/category-{id}', [app\web\controller\Goods::class, 'category']);
    }

    // 自动注册插件路由
    if (class_exists('app\common\plugin\PluginRouteManager')) {
        \app\common\plugin\PluginRouteManager::registerPluginRoutes();
    }

} // 结束安装检查的if语句

// 模板模式切换 - 根据配置决定使用vue还是html模板
Route::group('/', function () {
    Route::fallback(function () {
        // 检查是否已安装
        $lockFile = base_path() . '/.lock';
        if (!file_exists($lockFile)) {
            // 未安装，重定向到安装页面
            $path = request()->path();
            if (!str_starts_with($path, 'install')) {
                return redirect('/install');
            }
            // 如果是安装相关路径，直接返回404让具体的安装路由处理
            return response('', 404);
        }

        $siteMode = conf('app_site_mode');
        $path     = request()->path();

        // 如果是后台路径（以admin开头），总是使用后台vue 或者 adminapi/plugin
        if (strpos($path, 'admin') === 0 || strpos($path, '/admin') !== false || strpos($path, 'adminapi/plugin') !== false) {
            $viewPath = 'public/vue/index.html';
            return view($viewPath, [
                'title'       => '管理后台 - ' . conf('site_name'),
                'keywords'    => conf('site_keywords'),
                'description' => conf('site_desc'),
            ]);
        }

        // 根据站点模式处理前台路由
        if ($siteMode === 'html') {
            // HTML模式下，如果没有匹配到前面的路由，使用Index控制器处理404
            // return (new \app\web\controller\Index())->notFound();
            // 抛出 404 状态码
            return response('Not Found', 404);
        } else {
            // Vue模式下，所有未匹配的路由都返回Vue入口页面
            $viewPath = 'public/vue/index.html';
            return view($viewPath, [
                'title'       => conf('site_subtitle') . ' - ' . conf('site_name'),
                'keywords'    => conf('site_keywords'),
                'description' => conf('site_desc'),
            ]);
        }
    });
});
